<?php
namespace app\teacher\controller;

use app\teacher\model\Teacher;
use think\Request;
use think\Session;
class  Set extends Base
{
  //设置密码
    public function index(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
            "user"=>$user,
        );
        return view("index",$data);
    }
    public function updatePwd(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        //检测旧密码是否正确，如果正确这直接修改密码
        $res = Teacher::where(array("id"=>$user['id'],"password"=>md5($data['pwd1'])))->find();
        if($res){
            $ress = Teacher::update(array("id"=>$user['id'],"password"=>md5($data['pwd2'])));
            if($ress){
                Session::delete("teacher_infos");
                $this->success("修改密码成功，请重新登陆","Login/logins");
            }else{
                $this->error("修改失败");
            }
        }else{
            $this->error("旧密码错误");
        }
    }
    //设置个性签名
    public function signatrue(){
      $user = $this->user_info();
      $data = array(
        "setting" => $this->getAllSetting(),
        "user"=>$user,
      );
      return view("signature",$data);
    }

    //设置分享
  public function share(){
    $user = $this->user_info();
    $data = array(
      "setting" => $this->getAllSetting(),
      "user"=>$user,
    );
    return view("share",$data);
  }

  //修改
  public function update(Request $request){
    $user = $this->user_info();
    $data = $request->post();
    $data['id'] = $user["id"];
    $res = Teacher::update($data);
    if($res){
      $this->success("设置成功","My/index");
    }else{
      $this->error("设置成功");
    }
  }
  //设置头像
  public function header_photo(){
      $user = $this->user_info();
      $data = [
        "setting" => $this->getAllSetting(),
        "user" => $user
      ];
      return view("header_photo",$data);
  }
  public function update_photo(Request $request){
      $user = $this->user_info();
      $data = $request->post();
      $data['id'] = $user['id'];
      if(Teacher::update($data)){
        $this->success("保存成功");
      }else{
        $this->error("保存失败");
      }
  }

}
